/**
 * This trigger is assign the lead to a campaign when the 
 * owner of the lead is changed to campaign Owner_Assignment__c field.
 * It also removes the lead from the previous owners campaign.
 */
trigger UpdCampaignOnLeadOwnerChg on Lead (before update) {
		//This is implemented to eliminate executing the code twice for an instance.
	    if (!StopRecursion.DoRun)
	        return;
	    
	    system.debug('upd camp 1');
	    //ExLeadAssigRulesOnUpdate.callLeadRules(); 
	    
	    List<Lead> changedLeads = new List<Lead>();
	    Set<string> newOwnerIds = new Set<string>();
	    List<string> oldOwnerIds = new List<string>();
	    List<CampaignMember> newCampaignMembers = new List<CampaignMember>();
	        system.debug('upd camp 2');
	    for (Lead l : Trigger.new) {
	            system.debug('upd camp 3');
	        if (l.OwnerId != Trigger.oldMap.get(l.Id).OwnerId) {
	            system.debug('upd camp 4');
	            system.debug(l.OwnerId);
	            changedLeads.add(l);
	            string s = ((string)l.OwnerId).substring(0,15);
	            newOwnerIds.add(s);
	            system.debug(s);
	            s= ((string)Trigger.oldMap.get(l.Id).OwnerId).substring(0,15);
	            system.debug(s);
	            oldOwnerIds.add(s);
	        }
	    }
	    if (changedLeads.size() > 0) {
	    	system.debug('newOwnerids');
	    	system.debug(newOwnerids.size());
	        List<Campaign> newCampaigns = [SELECT c.Id, c.Owner_Assignment__c 
	                                       FROM Campaign c
	                                       WHERE c.Owner_Assignment__c IN :newOwnerIds]; //= '00Gc0000000QsN3'];
	        List<CampaignMember> cmRemoveList = [SELECT cm.Id 
	                                             FROM CampaignMember cm
	                                             WHERE cm.Campaign.Owner_Assignment__c IN :oldOwnerIds
	                                             AND cm.LeadId IN :Trigger.new];
	        system.debug('campaignMap.size()');
	        
	        system.debug(newCampaigns.size());
	        Map<Id, Campaign> campaignMap = new Map<Id, Campaign>();
	        for (Campaign c : newCampaigns) {
	            campaignMap.put(c.Owner_Assignment__c, c);
	        }
	        system.debug(' campagine data');
	        system.debug(campaignMap.size());
	        
	        for (Lead l : changedLeads) {
	            Campaign c = campaignMap.get(l.OwnerId);
	            system.debug(l.OwnerId);
	            system.debug(l.id);
	            system.debug('c.ownner id');
	            //system.debug(c.Id);
	            if (c != null) {
	                CampaignMember cm = new CampaignMember();
	                cm.CampaignId = c.Id;
	                system.debug(cm.CampaignId);
	                cm.LeadId = l.Id;
	                system.debug(cm.LeadId);
	                newCampaignMembers.add(cm);
	            }
	        }
	        if(newCampaignMembers.size()>0)
	        	insert newCampaignMembers;
	        if(cmRemoveList.size()>0)
	        	delete cmRemoveList;	        	
	        StopRecursion.DoRun = false;
	    }
    //if(StopRecursion.DoRun)
    //{     
    //  UpdateCampaignOnLeadOwnerChg upCamp = new UpdateCampaignOnLeadOwnerChg();
    //  upCamp.chgCampaignOnLeadOwnerChg(Trigger.new,Trigger.old);
    //  StopRecursion.DoRun = false;
    //}
    
}